<?php

require_once 'core.php';

/** 
 * @author giorgi chichinadze
 * 
 * Server Side Processing
 */
class SSP{
    
    public $query;
    public $queryString;
    
    public $limitStart;
    public $limitEnd;
    
    public $orderColumn;
    public $orderType;
    
    public $columnsList;
    
    public $search;

    /**
     * Constructor
     * 
     * @param string $query
     * @param array  $queryString
     */
    function SSP($query, $queryString)
    {
        
        $this->query        = $query;
        
        $this->start        = $queryString[start];
        $this->end          = $queryString[length];
        
        $this->orderColumn  = $queryString[order][0][column];
        $this->orderType    = $queryString[order][0][dir];
        
        $this->columns      = $queryString[columns];
        
        $this->search       = $queryString[search][value];
           
    }
    
    /**
     * getColumns
     * 
     * *get columns list from query
     * 
     * @return array 
     */
    function getColumns(){
    
        $beforeFrom         = explode("FROM", $this->query);
        $replaceCharacters  = array(" ", ",", "\t");
        $replacedString     = str_replace($replaceCharacters, "", $beforeFrom[0]);
        $replacedString     = substr($replacedString, 0, -1);
    
        $explodedLines      = explode("\n", $replacedString);
    
        $columnsArray       = array();
    
        foreach ($explodedLines as $value) {
    
            $afterAS = explode("AS", $value);
            $columnsArray[] = $afterAS[1];
    
        }
    
        return $columnsArray;
    
    }
    
    
    /**
     * limit
     * 
     * *limit rows
     * 
     * @return string
     */
    function limit(){
        
        $Limit = "";
        if ( isset( $this->start ) && $this->end != '-1' )
            {
                $Limit = "LIMIT ".intval( $this->start ).", ".intval( $this->end );
            }
            
        return $Limit;
        
    }
    
    /**
     * order
     * 
     **order rows 
     * 
     * @return string
     */
    function order() {
        
        $Order = "";
        if ( isset( $this->orderColumn ) ){
            
            $Order = "ORDER BY  ".self::getColumns()[$this->orderColumn] . " " .$this->orderType;   
                    
        }
        
        return $Order;
        
    }
    
    /**
     * filter
     * 
     * all columns filtering
     * 
     * @return string
     */
    function filter() {
        
        $Where = "";
        
        if ( $this->search != '' ){
            
            $Where = "HAVING (";
            for ( $i=0 ; $i<count($this->columns) ; $i++ ){
                    
                    $Where .= self::getColumns()[$i]." LIKE '%".mysql_real_escape_string( $this->search )."%' OR ";
                
            }
            
            $Where = substr_replace( $Where, "", -3 );
            $Where .= ')';
            
        }
        
        return $Where;
        
    }
    
    /**
     * individualFilter
     * 
     * individual columns filtering
     * 
     * @return string
     */
    function individualFilter() {
        
        $Where = "";
        
        for ( $i=0 ; $i<count($this->columns) ; $i++ ){
            
            if ( $this->columns[$i][searchable] == true && $this->columns[$i][search][value] != '' ){
                $Where .= " AND ";
                $Where .= self::getColumns()[$i]." LIKE '%".mysql_real_escape_string($this->columns[$i][search][value])."%' ";
            
            }
            
        }
        
        return $Where;
        
    }
    
    /**
     * getData
     * 
     * get json encoded data of result array
     * 
     * @return string
     */
    function getData() {
        
        $query              = $this->query;
        $filter             = self::Filter();
        $individualFilter   = self::individualFilter();
        $order              = self::order();
        $limit              = self::limit();
        
        $queryFiltered      ="$query
                              $filter
                              $order
                              $limit";
        
        $data[recordsTotal]     = mysql_num_rows(mysql_query($query));
        
        $recordsFiltered        = mysql_num_rows(mysql_query($queryFiltered));
        $data[recordsFiltered]  = $recordsFiltered;  

        $queryFilteredResult = mysql_query($queryFiltered);
        
        if($recordsFiltered > 0){
        
            while ($aRow = mysql_fetch_array($queryFilteredResult)){
        
                $data['data'][] = $aRow;
                
            }
            
        }else{
            
            $data['data'] = '';
            
        };
        
        return ($data);
        
    }
    

}

/* $query=" SELECT DISTINCT `realizations`.`id` AS realizations_id,
            `realizations`.`id` AS realizations_id,
              `realizations`.date as r_date,
            `realizations`.`CustomerID` AS c_date,
              `realizations`.`CustomerName` AS c_name,
              `realizations`.`CustomerAddress` AS adress,
            `realizations`.`CustomerPhone` AS phone,
            realizations.Customer1CCode AS counte,
              `realizations`.`Sum` AS jami
         FROM  `realizations`
         where  (LENGTH(CustomerID)=11 OR CustomerID='') 
             AND SUBSTRING(CustomerName,1,3)!='ი/მ' 
             AND SUBSTRING(CustomerName,1,3)!='შპს' 
             AND SUBSTRING(CustomerName,1,3)!='იმ.' 
             AND SUBSTRING(CustomerName,1,3)!='ი.მ' 
             AND SUBSTRING(CustomerName,1,3)!='ს/ს' 
             AND SUBSTRING(CustomerName,1,3)!='სს ' 
             AND SUBSTRING(CustomerName,1,3)!='ს.ს' 
             AND SUBSTRING(CustomerName,1,3)!='შ.პ' 
             AND SUBSTRING(CustomerName,1,3)!='იმ '
           GROUP BY realizations.Customer1CCode"; */


/* $data = new SSP($query, $_GET);

echo  $data->getData(); */
?>